package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/
 * @create 2025/7/28 11:02
 * @since JDK17
 */

public class Solution06 {
    public int minFlips(String str) {
        str = str.repeat(2);
        char[] s = str.toCharArray();
        int ans = Integer.MAX_VALUE, cnt = 0, l, r, n = s.length, m = n / 2;
        for (l = r = 0; r < n; r++) {
            if (s[r] % 2 == r % 2) {
                cnt++;
            }
            l = m - r - 1;
            if (l < 0) {
                continue;
            }
            ans = Math.min(ans, Math.min(cnt, m - cnt));
            if (s[l] % 2 == l % 2) {
                cnt--;
            }
        }
        return ans;
    }
}
